using System;
using System.Data;

using System.Configuration;

using System.Data.OleDb;
using Entity;

namespace SalesVMT
{
    public class CompanyDB
    {
        #region PUBLIC
        public static DataSet GetOne(int _Company_ID)
        {
            string _sql = "SELECT * FROM SalesVMT_Company WHERE COMPANY_ID = " + _Company_ID.ToString();
            return clsConnection.Query(_sql);
        }
        public static int Insert(CompanyInfo _CompanyInfo)
        {
            int retVal = 0;

            string _sql = "INSERT INTO SalesVMT_Company (Company_ID, Company_Code, Company_Name, Company_Address, Company_Tel, " +
                                    " Company_Fax, Company_Email, Company_Website, Company_Currency, Company_City, Company_PriceDifference)" +
                                    " VALUES (@Company_ID, @Company_Code, @Company_Name, @Company_Address, @Company_Tel, " +
                                    " @Company_Fax, @Company_Email, @Company_Website, @Company_Currency, @Company_City, @Company_PriceDifference)";

            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand(_sql, dbConn);
            dbCmd.Parameters.AddWithValue("@Company_ID", _CompanyInfo.Company_ID);
            dbCmd.Parameters.AddWithValue("@Company_Code", _CompanyInfo.Company_Code);
            dbCmd.Parameters.AddWithValue("@Company_Name", _CompanyInfo.Company_Name);
            dbCmd.Parameters.AddWithValue("@Company_Address", _CompanyInfo.Company_Address);
            dbCmd.Parameters.AddWithValue("@Company_Tel", _CompanyInfo.Company_Tel);
            dbCmd.Parameters.AddWithValue("@Company_Fax", _CompanyInfo.Company_Fax);
            dbCmd.Parameters.AddWithValue("@Company_Email", _CompanyInfo.Company_Email);
            dbCmd.Parameters.AddWithValue("@Company_Website", _CompanyInfo.Company_Website);
            dbCmd.Parameters.AddWithValue("@Company_Currency", _CompanyInfo.Company_Currency);
            dbCmd.Parameters.AddWithValue("@Company_City", _CompanyInfo.Company_City);
            dbCmd.Parameters.AddWithValue("@Company_PriceDifference", _CompanyInfo.Company_PriceDifference);
            
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                    retVal = _CompanyInfo.Company_ID;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Update(CompanyInfo _CompanyInfo)
        {
            string _sql = " UPDATE SalesVMT_Company SET" +
                            " Company_Code = @Company_Code, "+
                            " Company_Name = @Company_Name, "+
                            " Company_Address = @Company_Address, "+
                            " Company_Tel = @Company_Tel, "+
                            " Company_Fax = @Company_Fax, " +
                            " Company_Email = @Company_Email, "+
                            " Company_Website= @Company_Website, "+
                            " Company_Currency = @Company_Currency, " +
                            " Company_City = @Company_City, "+
                            " Company_PriceDifference = @Company_PriceDifference " +
                            " WHERE (Company_ID = " + _CompanyInfo.Company_ID.ToString() + ")";
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand(_sql, dbConn);
            
            dbCmd.Parameters.AddWithValue("@Company_Code", _CompanyInfo.Company_Code);
            dbCmd.Parameters.AddWithValue("@Company_Name", _CompanyInfo.Company_Name);
            dbCmd.Parameters.AddWithValue("@Company_Address", _CompanyInfo.Company_Address);
            dbCmd.Parameters.AddWithValue("@Company_Tel", _CompanyInfo.Company_Tel);
            dbCmd.Parameters.AddWithValue("@Company_Fax", _CompanyInfo.Company_Fax);
            dbCmd.Parameters.AddWithValue("@Company_Email", _CompanyInfo.Company_Email);
            dbCmd.Parameters.AddWithValue("@Company_Website", _CompanyInfo.Company_Website);
            dbCmd.Parameters.AddWithValue("@Company_Currency", _CompanyInfo.Company_Currency);
            dbCmd.Parameters.AddWithValue("@Company_City", _CompanyInfo.Company_City);
            dbCmd.Parameters.AddWithValue("@Company_PriceDifference", _CompanyInfo.Company_PriceDifference);

            bool retVal = false;
            try
            {
                dbConn.Open();
                if(dbCmd.ExecuteNonQuery()>0)
                retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
          
            
        }
        public static CompanyInfo GetInfo(int _Company_ID)
        {
            CompanyInfo retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            string _sql = "SELECT * FROM SalesVMT_Company WHERE COMPANY_ID = " + _Company_ID.ToString(); ;
            OleDbCommand dbCmd = new OleDbCommand(_sql, dbConn);

            OleDbDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CompanyInfo();
                    retVal.Company_ID = Convert.ToInt32(dr["Company_ID"]);
                    retVal.Company_Code = Convert.ToString(dr["Company_Code"]);
                    retVal.Company_Name = Convert.ToString(dr["Company_Name"]);
                    retVal.Company_Address = Convert.ToString(dr["Company_Address"]);
                    retVal.Company_Tel = Convert.ToString(dr["Company_Tel"]);
                    retVal.Company_Fax = Convert.ToString(dr["Company_Fax"]);
                    retVal.Company_Email = Convert.ToString(dr["Company_Email"]);
                    retVal.Company_Website = Convert.ToString(dr["Company_Website"]);
                    retVal.Company_Currency = Convert.ToString(dr["Company_Currency"]);
                    retVal.Company_City = Convert.ToString(dr["Company_City"]);
                    retVal.Company_PriceDifference = Convert.ToDecimal(dr["Company_PriceDifference"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
